<?php
App::uses('AppHelper', 'View/Helper');

/**
 * Search Helper class for wrapping FormHelper methods.
 *
 * @package 	smartyview
 * @subpackage	view.helper
 */
class SearchFormHelper extends AppHelper
{

	public $helpers = array('Html');

	/**
	 * Generate Search Url.
	 *
	 * @param array $add_fields    Adding fields.
	 * @param array $remove_fields Removing fields.
	 *
	 * @return string Search Url
	 */
	public function generateSearchUrl(array $add_fields, array $remove_fields=array())
	{
		$query_params = $this->request->query;
		$named_params = $this->request->named;
		foreach ($query_params as $key => $value) {
			$arr_tmp = explode(',', $value);
			if (!empty($add_fields[$key])) {
				$arr_tmp = array_merge($arr_tmp, (array)$add_fields[$key]);
				$arr_tmp = array_unique($arr_tmp);
				unset($add_fields[$key]);
			}
			if (!empty($remove_fields[$key])) {
				$arr_tmp = array_diff($arr_tmp, (array)$remove_fields[$key]);
			}
			if(empty($arr_tmp)) unset($query_params[$key]);
			$query_params[$key] = implode(',', $arr_tmp);
		}
		$query_params = array_merge($query_params, $add_fields);
		$url = array(
				'controller' => 'search',
    			'action' 	 => 'index',
    			'?' 		 => $query_params,
			   );
		$url = array_merge($url, $named_params);
		return $this->Html->url($url, true);
	}
	public function genSelectBox($name,$arrValue,$key1,$id='',$class='')
	{
			$strSelect='<select name="'.$name.'" id="'.$name.$id.'"' .$class.'>';

			foreach($arrValue as $key=>$value) {
			  $check ='';
			  if($key==$key1) $check=' selected="selected"';
              $strSelect.='<option value="'.$key.'"'.$check.'>'.$value.'</option>';
            }
            $strSelect.='</select>';
            return $strSelect;
	}

	/**
	 * Generate ParamUrl
	 *
	 * @return string Url
	 */
	public function generateParamUrl()
	{
		if(isset($this->request->query) && !empty($this->request->query) && is_array($this->request->query))
		{
			$url= '?';
			foreach($this->request->query as $key=>$value)
			{
				//if($key=='keywords') $value =htmlentities($value);
				$url.=$key.'='.$value.'&';
			}
			return substr($url,0,-1);
		}else return '';
	}
	/**
	 * Generate ParamUrl
	 *
	 * @return string Url
	 */
	public function generateParamUrlAjax()
	{
		if(isset($this->request->data) && !empty($this->request->data) && is_array($this->request->data))
		{
			$url= '?';
			foreach($this->request->data as $key=>$value)
			{
				//if($key=='keywords') $value =htmlentities($value);
				$url.=$key.'='.$value.'&';
			}
			return substr($url,0,-1);
		}else return '';
	}
	/**
	 * Generate ParamUrl
	 *
	 * @return string Url
	 */
	public function generateParamUrlFromPost()
	{
		if(isset($this->request->data) && !empty($this->request->data) && is_array($this->request->data))
		{
			$url= '?';
			foreach($this->request->data as $key=>$value)
			{
				if(!is_array($value)){
					//if($key=='keywords') $value =htmlentities($value);
					$url.=$key.'='.$value.'&';
				}

			}
			return substr($url,0,-1);
		}else return '';
	}

	/**
	 * Generate ParamUrl
	 *
	 * @return string Url
	 */
	public function generateParamUrlCake()
	{
		if(isset($this->request->query) && !empty($this->request->query) && is_array($this->request->query))
		{
			$url= '';
			foreach($this->request->query as $key=>$value)
			{
				if(!in_array($key, array('keywords','Submit')))
				{
					//if($key=='keywords') $value =htmlentities($value);
					if(in_array($key, array('stayDateOut','stayDate')))
					$value=str_replace('/','-',substr($value,0,10));
					$url.=$key.':'.$value.'/';
				}
			}
			return substr($url,0,-1);
		}else return '';
	}
	public function generateCheckBoxUrl($invalue)
	{

		if(isset($this->request->query) && !empty($this->request->query) && is_array($this->request->query))
		{
			$url= '?';
			foreach($this->request->query as $key=>$value)
			{

				if($key=='allAvailable') $url.=$key.'='.$invalue.'&';
				//if($key=='keywords') $value =htmlentities($value);
				$url.=$key.'='.$value.'&';
			}
			return substr($url,0,-1);
		}else return '';
	}
	/**2
	 * get Default Date.
	 *
	 * @return string defaulDate
	 */
	public function generateDate($date='')
	{
		$date=$this->buildDate($date);
		//var_dump($date);
		$arrJanpane=array('Sun'=>'日','Mon'=>'月','Tue'=>'火','Wed'=>'水', 'Thu'=>'木', 'Fri'=>'金','Sat'=>'土');
		$key=date('D',strtotime($date));
		if( array_key_exists($key,$arrJanpane))
		return $date.' ('.$arrJanpane[$key].')';else  return $date.' ('.$arrJanpane[0].')';

	}

	public function buildDate($date)
    {
      if (!empty($date)) {
        $safeDate=substr(trim($date),0,10);
        $arrDate =explode('-',$safeDate,3);

        if(count($arrDate)==3) {
          if(checkdate(intval($arrDate[1]),intval($arrDate[2]),intval($arrDate[0]))==true) {
            if($safeDate < date('Y-m-d')) return date('Y/m/d');

              return implode('/', $arrDate);
          }else return date('Y/m/d');
        } else return date('Y/m/d');
      }
      return date('Y/m/d');
    }
    public function buildTime($time)
    {
      if (!empty($time)) {
        $safeDate=substr(trim($time),0,8);
        $arrDate =explode(':',$safeDate,3);
        if(count($arrDate)==3) 
        if(intval($arrDate[0]) > 0)  
              return $arrDate[0].':'.$arrDate[1];
          
      }
      return '';
    }
    public function selectBox($name,$arrValue,$key1,$id='',$class='')
    {
    	$strSelect='<select name="'.$name.'" id="'.$id.'" class="' .$class.'">';

    	foreach($arrValue as $key=>$value) {
    		$check ='';
    		if($key==$key1) $check=' selected="selected"';
    		$strSelect.='<option value="'.$key.'"'.$check.'>'.$value.'</option>';
    	}
    	$strSelect.='</select>';
    	return $strSelect;
    }
//caolp Build Japan Date
	public function generateJDate($date='')
	{
		$date=$this->buildDate($date);
		$arrJanpane=array('Sun'=>'日','Mon'=>'月','Tue'=>'火','Wed'=>'水', 'Thu'=>'木', 'Fri'=>'金','Sat'=>'土');
		$key=date('D',strtotime($date));
		$JDate=date('Y年m月d日',strtotime($date));
		if( array_key_exists($key,$arrJanpane))
			return $JDate.' ('.$arrJanpane[$key].')';else  return $date.' ('.$arrJanpane[0].')';

	}
}
